<template>
  <div style="grid-row: 2/3;">
    <div style="height: 100%;width: 100%; overflow: scroll; box-sizing: border-box;padding: 20px">
      <div style="display: flex; flex-direction: column;row-gap: 20px;">
        <div v-for="sentence of commonsentence" :key="sentence.id" style="display: flex; flex-direction: column;align-items: flex-start;
                  border: 1px solid red; border-radius: 20px; background-color: pink; row-gap: 20px; font-size: 50px;
                  position: relative; padding: 20px" @click="editItem(sentence)">
          <div style="display: flex; flex-direction: column;row-gap: 5px">
            <div>{{sentence.enQuestion}}</div>
            <div>{{sentence.zhQuestion}}</div>
          </div>
          <div style="display: flex; flex-direction: column;row-gap: 5px">
            <div>{{sentence.enAsk}}</div>
            <div>{{sentence.zhAsk}}</div>
          </div>
          <div class="remove" v-if="isEdit" style="position: absolute; top: 10px; right: 10px; font-size: 50px" @click.stop="remove(sentence)">
            <i class="el-icon-error"></i>
          </div>
        </div>
      </div>
      <el-dialog title="添加基础句型" :visible.sync="form.isShow">
        <el-form :model="form.data" ref="form">
          <el-form-item label="英文问句" prop="enQuestion">
            <el-input v-model="form.data.enQuestion"></el-input>
          </el-form-item>
          <el-form-item label="英文问句翻译" prop="zhQuestion">
            <el-input v-model="form.data.zhQuestion"></el-input>
          </el-form-item>
          <el-form-item label="英文答句" prop="enAsk">
            <el-input v-model="form.data.enAsk"></el-input>
          </el-form-item>
          <el-form-item label="英文答句翻译" prop="zhAsk">
            <el-input v-model="form.data.zhAsk"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="form.isShow = false">取 消</el-button>
          <el-button type="primary" @click="formEnter">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div style="position: fixed; right: 20px; top: 20px">
      <el-button @click="edit()">{{isEdit ? '取消编辑' : '编辑'}}</el-button>
      <el-button @click="add()">添加</el-button>
    </div>
  </div>
</template>

<script>
import {insertCommonSentence, selectCommonSentence, deleteCommonSentence, updateCommonSentence} from "@/api/controller";

export default {
  name: "ViewCommonsentence",
  methods: {
    editItem(sentence) {
      if (!this.isEdit) {
        return
      }
      this.form.isShow = true
      this.resetForm()
      this.form.data = {
        enQuestion: sentence.enQuestion,
        zhQuestion: sentence.zhQuestion,
        enAsk: sentence.enAsk,
        zhAsk: sentence.zhAsk
      }
      this.id = sentence.id
    },
    resetForm() {
      this.form.data = {
        enQuestion: '',
        zhQuestion: '',
        enAsk: '',
        zhAsk: ''
      }
    },
    async remove(sentence) {
      try {
        await this.$confirm('此操作将永久删除该常见字, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        try {
          await deleteCommonSentence(sentence)
          await this.selectCommonSentence()
          this.$message({
            type: 'success',
            message: `基础句型删除成功!`
          });
        } catch(error) {
          this.$message({
            type: 'error',
            message: `基础句型删除失败!`
          });
        }
      } catch(error) {
        console.log(error)
      }
    },
    async selectCommonSentence() {
      try {
        let result = await selectCommonSentence()
        this.commonsentence = result.data
      } catch (error) {
        console.log(error)
      }
    },
    async formEnter() {
      try {
        if (!this.id) {
          await insertCommonSentence(this.form.data)
          this.form.isShow = false
          await this.selectCommonSentence()
        } else {
          await updateCommonSentence({...this.form.data, id: this.id})
          this.form.isShow = false
          await this.selectCommonSentence()
        }
      } catch(error) {
        console.log(error)
      }
    },
    add() {
      this.form.isShow = true
      this.id = null
      this.resetForm()
    },
    edit() {
      this.isEdit = !this.isEdit
    }
  },
  async mounted() {
    await this.selectCommonSentence()
  },
  data() {
    return {
      id: '',
      form: {
        isShow: false,
        data: {
          enQuestion: '',
          zhQuestion: '',
          enAsk: '',
          zhAsk: ''
        },
      },
      isEdit: false,
      commonsentence: []
    }
  }
}
</script>

<style scoped>
.remove:hover {
  cursor: pointer;
  color: red;
}
</style>